//
//  Client_RF225_qPalletController.swift
//  Wmsmobile
//
//  Created by xuNick on 16/10/9.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class Client_RF225_qPalletController: HDFounctionBaseController , HDBarcodeInputCellInputDelegate {
    //MARK: - property
    var dataArray = [
        ["托盘条码"],
        ["货品码" , "货品名称" , "规格" , "生产日期" , "保质期" , "件数" , "托盘状态"]
    ]
    override var firstActiveIndexPath: IndexPath?{
        set{
        }
        get{
            return IndexPath(item: 0, section: 0)  //在这里添加需要进入界面就激活的cell的位置
        }
    }
    
    //MARK: - delegate
    
    func clicKReturn(_ cell: HDBarcodeInputCell) {
        if cell.codeInputView.textField.text == nil {
            self.showMessage("请输入托盘条码", success: false)
            return
        }
        self.getData(["piPalletCode" : self.getText(0, inSection: 0) ?? ""], bodyMethod: "qryPallet", suffix: nil, success: { (dict) in
            //展示数据
            self.setText(0, inSection: 1, text: dict["poArticleCode"])
            self.setText(1, inSection: 1, text: dict["poArticleName"])
            self.setText(2, inSection: 1, text: dict["poSpec"])
            let dateString = (dict["poProduceDate"] ?? "").components(separatedBy: "T").first
            self.setText(3, inSection: 1, text: dateString)
            self.setText(4, inSection: 1, text: dict["poValidPeriod"])
            self.setText(5, inSection: 1, text: dict["poQpc"])
            self.setText(6, inSection: 1, text: dict["poStat"])
            cell.codeInputView.textField.text = nil
            cell.updateButtonState()
            }) { (dict) in
                
        }
    }
    //MARK: - private
    override func isEmpty() -> (Bool) { //判断控制器是否为空，必须要实现
        return true
    }
    //MARK: - tableviewDatasource
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return dataArray[section].count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if let cell = cellsDict[String(describing: indexPath)] {
                return cell
            }else{
                let position = (indexPath as NSIndexPath).section * 100 + (indexPath as NSIndexPath).row
                var cell: HDFounctionBaseCell?
                switch position {
                case 0:
                    cell = HDBarcodeInputCell()
                    (cell as! HDBarcodeInputCell).InputDelegate = self
                default:
                    cell = HDProductInfocell()
                    break
                }
                cell?.titleView.text = dataArray[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
                cellsDict.updateValue(cell!, forKey: String(describing: indexPath))
                return cell!

            }
    }
    
    //MARK: - tableViewDelegate
//    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        switch section {
//        case 1:
//            let view = FounctionSectionHeaderView(headerString: "货品,规格,件数")
//            return view
//        default:
//            return nil
//        }
//    }
//    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        switch section {
//        case 0:
//            return 10
//        case 1:
//            return HDFSectionHeadrHeight
//        default:
//            return 0
//        }
//    }
//    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return 10
//    }
//    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//        if indexPath.section == 1 && dataArrayS2 != nil {
//            let dict = self.dataArrayS2![indexPath.row]
//            let vc = chkBinRtnArtController(info: dict)
//            self.navigationController?.pushViewController(vc, animated: true)
//        }
//        
//    }
    
}
